package cn.edu.zut.cs.Controller;

import cn.edu.zut.cs.Business.CommunityService;
import cn.edu.zut.cs.Model.Community;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@Controller
public class communityController {

    CommunityService service;

    @RequestMapping(value = "/index.html")
    public String index(){
        return "index";
    }

    @RequestMapping(value = "/peolist.html")
    public String peolist(HttpServletRequest request){
        request.getSession().setAttribute("people",service.peoCommunity());
        return "peolist";
    }

    @RequestMapping(value = "/cglist.html")
    public String cglist(HttpServletRequest request){
        request.getSession().setAttribute("people",service.peoManagement());
        return "cglist";
    }

    @RequestMapping(value = "/newcg.html")
    public String newcg(){
        return "newcg";
    }

    @RequestMapping(value = "/insert.html")
    public String insertCommunity(HttpServletRequest request, Community community){
        community.setTime(new Date());
        System.out.println(community);
        if(community.getState()==1){
            service.insertCommunity(community);
        }
        else if(community.getState()==0){
            service.insertpeoManagement(community);
            service.deletepeoCommunity(community);
        }
        request.getSession().setAttribute("people",service.peoManagement());
        return "cglist";
    }

    @Autowired
    public void setService(CommunityService service){
        this.service=service;
    }
}
